package com.example.boot02.controller;

import com.example.boot02.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Controller
public class UserController {
    @RequestMapping("/login")
    @ResponseBody
    public String login(String username,String password){
        try(
                Connection con = DBUtil.getConnection();
                ) {
            String sql = "SELECT password FROM user WHERE username=?";
            PreparedStatement pst = con.prepareStatement(sql);
            pst.setString(1,username);
            ResultSet res = pst.executeQuery();
            if(res.next()){
                String pw = res.getString(1);
                if(pw.equals(password)){
                    return "登录成功";
                }
                return "密码错误";
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "用户不存在";
    }
}
